package cn.com.headfree.depterprela.controller;

import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gta.edu.sdk.common.dto.Page;

import cn.com.headfree.base.controller.ExBaseController;
import cn.com.headfree.base.controller.ResultData;
import cn.com.headfree.depterprela.service.IDeptErpRelaService;
import cn.com.headfree.entity.UserDeptERpRelaMsg;
import cn.com.headfree.entity.isc.User;

/**
 * @Title  DeptErpRelaController.java
 * @Package cn.com.headfree.depterprela.controller
 * @Description 用户部门Erp关联信息controller
 * @author yangwang
 * @date  2020年7月3日 下午5:07:10
 */
@Controller
@RequestMapping(value = "/deptErpRela", produces = "application/json;charset=UTF-8")
public class DeptErpRelaController extends ExBaseController {
	private static final Logger LOG = LogManager.getLogger(DeptErpRelaController.class);

	@Autowired
	private IDeptErpRelaService deptErpRelaService;
	/**
	 * 配置环境
	 *  */
	@Value("${spring.profiles.active}")
	private String spring_profiles_active;

	/**
	 * 获取部门下所有人员
	 * @param page
	 * @return ResultData
	 *  */
	@RequestMapping("/getUsersOfDeptPage")
    @ResponseBody
    public ResultData getUsersOfDeptPage(HttpServletRequest request,Page<UserDeptERpRelaMsg> page,String userName) {
		ResultData result = new ResultData();
		try {
			//获取用户登录信息
			User user = this.getLoginUserInfo(request);
			//测试数据--start
//			if(StringUtils.equals(spring_profiles_active, "dev")) {
//				user = new User();
//				user.setId("39AE8ED108BB00EAE0530A8A018B00EA");
//				user.setName("程媛媛");
//				user.setBaseOrgId("10342674");
//				user.setOrgAnisationName("营销业务系统");
//				user.setFax("30132685");
//			}
	        //测试数据--end
			if((null != user)&&(StringUtils.isNotBlank(user.getId()))) {
				//获取登陆人部门下所有人员
				deptErpRelaService.getUsersOfDeptPage(page, new UserDeptERpRelaMsg(userName,user.getBaseOrgId()));
				result.setData(page);
				LOG.info("获取部门下所有人员信息成功");
			}else {
				result.setErrorMsg("获取用户登录信息失败");
	            result.setSuccess(false);
	            LOG.error("获取用户登录信息失败");
			}
        } catch (Exception e) {
            result.setErrorMsg("获取部门下所有人员信息异常");
            result.setSuccess(false);
            LOG.error("获取部门下所有人员信息异常", e);
        }
        return result;
	}

	/**
	 * 根据信息分类id获取角色下所有用户信息
	 * @author yangwang
	 * @param businessTypeId 信息分类id
	 * @return ResultData
	 * @throws SQLException
	 *  */
	@RequestMapping("/getUsersByBusinessTypeId")
    @ResponseBody
    public ResultData getUsersByBusinessTypeId(String businessTypeId){
		ResultData result = new ResultData();
		try {
			return this.getUsersByBusinessTypeId(businessTypeId);
        } catch (Exception e) {
            result.setErrorMsg("根据信息分类id获取角色下所有用户信息异常");
            result.setSuccess(false);
            LOG.error("根据信息分类id获取角色下所有用户信息异常", e);
        }
        return result;
	}

}
